package com.cloudbroker.bcs.common.constants.type;

import java.util.Calendar;

public enum PeriodType {
    
    YEAR(1, "年", Calendar.YEAR),
    
    MONTH(2, "月", Calendar.MONTH),
    
    WEEK(3, "周", Calendar.WEEK_OF_YEAR),
    
    DAY(4, "日", Calendar.DAY_OF_MONTH),
    
    HOUR(5, "时", Calendar.HOUR_OF_DAY),
    
    MINUTE(6, "分", Calendar.MINUTE),
    
    SECOND(7, "秒", Calendar.SECOND),
    
    MILLISECOND(8, "毫秒", Calendar.MILLISECOND),
    
    ;
    
    private final int val;
    
    private final String description;
    
    private String toString;
    
    private final int calendarField;
    
    private PeriodType(int val, String description, int calendarField) {
        this.val = val;
        this.description = description;
        this.calendarField = calendarField;
    }
    
    public static PeriodType fromCalendarField(int calendarField) {
        switch (calendarField) {
            case Calendar.YEAR:
                return YEAR;
            case Calendar.MONTH:
                return MONTH;
            case Calendar.WEEK_OF_YEAR:
            case Calendar.WEEK_OF_MONTH:
                return WEEK;
            case Calendar.DAY_OF_MONTH:
            case Calendar.DAY_OF_YEAR:
            case Calendar.DAY_OF_WEEK:
            case Calendar.DAY_OF_WEEK_IN_MONTH:
                return DAY;
            case Calendar.HOUR_OF_DAY:
                return HOUR;
            case Calendar.MINUTE:
                return MINUTE;
            case Calendar.SECOND:
                return SECOND;
            case Calendar.MILLISECOND:
                return MILLISECOND;
            default:
                return null;
        }
    }
    
    public int getVal() {
        return val;
    }
    
    public String getDescription() {
        return description;
    }
    
    public int getCalendarField() {
        return calendarField;
    }
    
    @Override
    public String toString() {
        if (null == toString) {
            toString = new StringBuilder().append("PeriodType[").append(val).append(':').append(description)
                    .append(']').toString();
        }
        return toString;
    }
    
}
